import tkinter as tk
from tkinter import font

def create_receipt_window():
    # 创建主窗口
    window = tk.Toplevel()
    window.title("购物小票")
    window.geometry("350x600")
    window.resizable(False, False)
    
    # 设置字体，使用等宽字体更适合小票
    receipt_font = font.Font(family="Courier New", size=10)
    
    # 创建文本框用于显示小票内容
    receipt_text = tk.Text(window, font=receipt_font, wrap=tk.WORD, padx=10, pady=10)
    receipt_text.pack(fill=tk.BOTH, expand=True)
    receipt_text.config(state=tk.DISABLED)  # 禁止编辑
    
    # 定义小票内容
    receipt_info = {
        "store_name": "电子产品商店",
        "order_number": "DH202311010001",
        "date_time": "2023-11-01 20:56:15",
        "items": [
            {"name": "金士顿U盘8G", "quantity": 1, "price": 40.00},
            {"name": "胜创16GTF卡", "quantity": 1, "price": 50.00},
            {"name": "读卡器", "quantity": 1, "price": 8.00},
            {"name": "网线2米", "quantity": 1, "price": 5.00},
            {"name": "键盘", "quantity": 1, "price": 500.00}
        ],
        "cashier": "管理员",
        "discount": 0.8  # 八折折扣
    }
    
    # 计算总额
    total_amount = sum(item["quantity"] * item["price"] for item in receipt_info["items"])
    total_quantity = sum(item["quantity"] for item in receipt_info["items"])
    
    # 计算折扣后金额
    discount = receipt_info["discount"]
    discounted_amount = total_amount * discount
    
    # 生成小票内容1
    receipt_lines = []
    line_length = 40
    
    # 标题和分隔线
    receipt_lines.append("=" * line_length)
    receipt_lines.append(receipt_info["store_name"].center(line_length))
    receipt_lines.append(f"折扣: {int(discount * 10)}折".center(line_length))
    receipt_lines.append("=" * line_length)
    
    # 订单信息
    receipt_lines.append(f"单号: {receipt_info['order_number']}")
    receipt_lines.append(f"时间: {receipt_info['date_time']}")
    receipt_lines.append("-" * line_length)
    
    # 商品列表标题
    receipt_lines.append(f"{'名称':<18} {'数量':<6} {'单价':<6} {'金额':<6}")
    receipt_lines.append("-" * line_length)
    
    # 商品列表
    for item in receipt_info["items"]:
        amount = item["quantity"] * item["price"]
        receipt_lines.append(
            f"{item['name']:<18} {item['quantity']:<6} {item['price']:<6.2f} {amount:<6.2f}"
        )
    
    # 总计信息
    receipt_lines.append("-" * line_length)
    receipt_lines.append(f"总数: {total_quantity}")
    receipt_lines.append(f"总额: {total_amount:.2f}")
    receipt_lines.append(f"折扣: {int(discount * 10)}折")
    receipt_lines.append(f"折后总额: {discounted_amount:.2f}")
    receipt_lines.append(f"实收: {discounted_amount:.2f}")
    receipt_lines.append(f"找零: 0.00")
    receipt_lines.append("-" * line_length)
    
    # 收银员信息
    receipt_lines.append(f"收银: {receipt_info['cashier']}")
    receipt_lines.append("=" * line_length)
    receipt_lines.append("感谢光临，欢迎下次再来！".center(line_length))
    
    # 将内容插入文本框
    receipt_text.config(state=tk.NORMAL)
    for line in receipt_lines:
        receipt_text.insert(tk.END, line + "\n")
    receipt_text.config(state=tk.DISABLED)
    
    return window

if __name__ == "__main__":
    root = tk.Tk()
    root.withdraw()  # 隐藏主窗口
    receipt_window = create_receipt_window()
    root.mainloop()
    